package com.youlu.campus.service.qa;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.youlu.campus.entity.ActivityAnswerInfo;
import com.youlu.campus.entity.ActivityQuestionInfo;
import com.youlu.campus.entity.VO.res.UserQaAnswerLogExport;
import com.youlu.campus.entity.english.UserQaAnswerLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserQaAnswerLogService {

    @Autowired
    private MongoTemplate mongoTemplate;


    public void userQaLogExport(String activityId){
        log.info("【用户答题日志导出】activityId->{}",activityId);
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        List<UserQaAnswerLog> logList = this.mongoTemplate.find(query, UserQaAnswerLog.class);
        if(CollectionUtils.isEmpty(logList)){
            return;
        }
        final String qaTypeRadio = "单选";
        final String qaTypeCheck = "多选";

        List<UserQaAnswerLogExport> exportList = new ArrayList<>();
        Map<String, List<UserQaAnswerLog>> mapQa = logList.stream().filter(userQaAnswerLog -> !StringUtils.isEmpty(userQaAnswerLog.getName()) && !StringUtils.isEmpty(userQaAnswerLog.getUniversityName())).collect(Collectors.groupingBy(UserQaAnswerLog::getUserId));
        mapQa.forEach((user,item)->{
            UserQaAnswerLog userQaAnswerLog = item.stream().sorted(Comparator.comparing(UserQaAnswerLog::getScore).reversed()).findFirst().orElse(null);
            if(Objects.nonNull(userQaAnswerLog)){
                log.info("【用户答题日志导出】name -> {},activityId->{}",userQaAnswerLog.getName(),activityId);
                UserQaAnswerLogExport userQaAnswerLogExport = new UserQaAnswerLogExport();
                userQaAnswerLogExport.setName(userQaAnswerLog.getName());
                userQaAnswerLogExport.setUniversityName(userQaAnswerLog.getUniversityName());
                userQaAnswerLogExport.setScore(userQaAnswerLog.getScore().toString());
                final Integer[] index = {1};
                List<ActivityQuestionInfo> questionInfoList = userQaAnswerLog.getAnswersQuestionInfoList().stream().sorted(Comparator.comparing(ActivityQuestionInfo::getPriority)).collect(Collectors.toList());
                questionInfoList.forEach(activityQuestionInfo -> {

                    List<ActivityAnswerInfo> userAnswerCheckList = activityQuestionInfo.getAnswerInfos().stream().filter(activityAnswerInfo -> activityAnswerInfo.getAnswered()).collect(Collectors.toList());
//                    StringBuilder userQa = new StringBuilder(0);
//                    userAnswerCheckList.forEach(activityAnswerInfo -> {
//                        if(userQa.length()>0){
//                            userQa.append(",").append(activityAnswerInfo.getPriority());
//                        }else{
//                            userQa.append(activityAnswerInfo.getPriority());
//                        }
//                    });
                    String userQa =
                            userAnswerCheckList.stream().map(ActivityAnswerInfo::getTitle).collect(Collectors.joining(","));
                    switch (index[0]){
                        case 1:
                            log.info("【用户答题日志导出】1 name -> {},activityId->{} index ->{}",userQaAnswerLog.getName(),
                                    activityId, index[0]);
                            userQaAnswerLogExport.setQa1(userQa);
                            break;
                        case 2:
                            log.info("【用户答题日志导出】2 name -> {},activityId->{}",userQaAnswerLog.getName(),activityId);
                            userQaAnswerLogExport.setQa2(userQa);
                            break;
                        case 3:
                            log.info("【用户答题日志导出】3 name -> {},activityId->{}",userQaAnswerLog.getName(),activityId);
                            userQaAnswerLogExport.setQa3(userQa);
                            break;
                        case 4:
                            userQaAnswerLogExport.setQa4(userQa);
                            break;
                        case 5:
                            userQaAnswerLogExport.setQa5(userQa);
                            break;
                        case 6:
                            userQaAnswerLogExport.setQa6(userQa);
                            break;
                        case 7:
                            userQaAnswerLogExport.setQa7(userQa);
                            break;
                        case 8:
                            userQaAnswerLogExport.setQa8(userQa);
                            break;
                        case 9:
                            userQaAnswerLogExport.setQa9(userQa);
                            break;
                        case 10:
                            userQaAnswerLogExport.setQa10(userQa);
                            break;
                        case 11:
                            userQaAnswerLogExport.setQa11(userQa);
                            break;
                        case 12:
                            userQaAnswerLogExport.setQa12(userQa);
                            break;
                        case 13:
                            userQaAnswerLogExport.setQa13(userQa);
                            break;
                        case 14:
                            userQaAnswerLogExport.setQa14(userQa);
                            break;
                        case 15:
                            userQaAnswerLogExport.setQa15(userQa);
                            break;
                        case 16:
                            userQaAnswerLogExport.setQa16(userQa);
                            break;
                        case 17:
                            userQaAnswerLogExport.setQa17(userQa);
                            break;
                            case 18:
                                userQaAnswerLogExport.setQa18(userQa);
                            break;
                        case 19:
                            userQaAnswerLogExport.setQa19(userQa);
                            break;
                        case 20:
                            userQaAnswerLogExport.setQa20(userQa);
                            break;
                    }
                    index[0]++;

                });
                exportList.add(userQaAnswerLogExport);

            }
        });
        //writeSheet = EasyExcel.writerSheet("").build();
        //excelWriter.write(exportList, writeSheet);
        ExcelWriter excelWriter = null;
        WriteSheet writeSheet = null;
        String fileName = "心理测评活动" + System.currentTimeMillis() + ExcelTypeEnum.XLSX.getValue();
        String file = "/data/app/file/" + fileName;
        excelWriter = EasyExcel.write(file, UserQaAnswerLogExport.class).build();
        try {
            writeSheet = EasyExcel.writerSheet("").build();
            excelWriter.write(exportList, writeSheet);
        }catch (Exception e){
            log.error("导出错误",e);
        }finally {
            if (Objects.nonNull(excelWriter)) {
                excelWriter.finish();
            }
        }

    }

    public static void main(String[] args) {

    }
}
